<%-- 
    Document   : Pembelian
    Created on : May 25, 2013, 10:09:33 PM
    Author     : Florencia
--%>

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.sql.Statement"%>
<%@page import="bean.Barang"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="bean.Pembelian"%>
<%@page import="java.util.logging.Logger"%>
<%@page import="java.util.logging.Level"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.Connection"%>
<%@page import="bean.OraConnection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Data Transaksi</title>
    </head>
    <body background = "gambar/background1.jpg" style = "background-repeat: no-repeat; background-attachment: fixed">
        <%

                    String urlHome = response.encodeURL("index.jsp");
                    out.println("<h1><a href ='" + urlHome + "' style=text-decoration:none>HOME</a>&nbsp;&nbsp;</h1>");
                    out.println("<br>");
                    bean.Pembelian pembelian = (bean.Pembelian) session.getAttribute("pembelian");
                    if (pembelian == null) {
                        pembelian = new bean.Pembelian();
                        session.setAttribute("pembelian", pembelian);
                    }
                    Date date = new Date();
                    SimpleDateFormat tanggal = new SimpleDateFormat("dd-MM-yyyy");
                    pembelian.setTanggal(tanggal.format(date));

                    Connection conn12 = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "115314023");
                    java.sql.Statement statement12 = null;
                    try {
                        statement12 = conn12.createStatement();
                        String sqQ12 = "select count(nomornota) from nota";
                        ResultSet result12 = null;
                        result12 = statement12.executeQuery(sqQ12);
                        while (result12.next()) {
                            String no = result12.getString("count(nomornota)");
                            int no1 = Integer.parseInt(no);
                            no1 = no1 + 1;
                            pembelian.setNomornota(no1);
                        }
                        conn12.close();
                    } catch (SQLException ex) {
                    }


                    pembelian.setKtp((String) request.getParameter("ktp"));
                    pembelian.setNama((String) request.getParameter("nama"));
                    pembelian.setAlamat((String) request.getParameter("alamat"));
                    pembelian.setNohp((String) request.getParameter("nohp"));


                    Connection conn6 = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "115314023");
                    Statement statement6 = null;
                    try {
                        statement6 = conn6.createStatement();
                        String sql6 = "insert into DATAPEMBELI (ktp, nama,alamat,nohp) values ('"
                                + pembelian.getKtp() + "','" + pembelian.getNama() + "','"
                                + pembelian.getAlamat() + "','" + pembelian.getNohp()
                                + "')";
                        statement6.executeUpdate(sql6);
                        conn6.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(Pembelian.class.getName()).log(Level.SEVERE, null, ex);
                    }


                    pembelian.setId((String[]) request.getParameterValues("barang"));
                    out.println("<b>");
                    out.println("<center>");
                    out.println("<h2>Nama Pembeli      : " + pembelian.getNama() + "</h2>");
                    out.print("<table border = '5' cellspacing = '3' cellpadding = '3'>");
                    out.println("<tr>");
                    out.println("<td>ID BARANG</td>");
                    out.println("<td>NAMA BARANG</td>");
                    out.println("<td>STOK TERSISA</td>");
                    out.println("<td>HARGA BARANG</td>");
                    out.println("</tr>");

                    pembelian.setHarga_total(0);
                    pembelian.setJumlah_barang(0);
                    if (pembelian.getId() != null) {
                        for (int i = 0; i < pembelian.getId().length; i++) {
                            if (pembelian.getId()[i].equals("1") || pembelian.getId()[i].equals("2") || pembelian.getId()[i].equals("3")
                                    || pembelian.getId()[i].equals("4") || pembelian.getId()[i].equals("5")
                                    || pembelian.getId()[i].equals("6")) {
                                pembelian.setJenis_barang("tas");
                            } else if (pembelian.getId()[i].equals("7") || pembelian.getId()[i].equals("8") || pembelian.getId()[i].equals("9")
                                    || pembelian.getId()[i].equals("10") || pembelian.getId()[i].equals("11")
                                    || pembelian.getId()[i].equals("12")) {
                                pembelian.setJenis_barang("aksesoris");
                            } else if (pembelian.getId()[i].equals("13") || pembelian.getId()[i].equals("14") || pembelian.getId()[i].equals("15")
                                    || pembelian.getId()[i].equals("16") || pembelian.getId()[i].equals("17")
                                    || pembelian.getId()[i].equals("18")) {
                                pembelian.setJenis_barang("pakaian");
                            } else if (pembelian.getId()[i].equals("19") || pembelian.getId()[i].equals("20") || pembelian.getId()[i].equals("21")
                                    || pembelian.getId()[i].equals("22") || pembelian.getId()[i].equals("23")
                                    || pembelian.getId()[i].equals("24")) {
                                pembelian.setJenis_barang("sepatu");
                            }


                            Connection conn3 = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "115314023");
                            Statement statement3 = null;
                            try {
                                statement3 = conn3.createStatement();
                                for (int j = 0; j < pembelian.getId().length; j++) {
                                    String sql3 = "update barang set stok = stok - 1 where id_barang = " + pembelian.getId()[i];
                                    statement3.executeUpdate(sql3);
                                    conn3.close();
                                }
                            } catch (SQLException ex) {
                                Logger.getLogger(Pembelian.class.getName()).log(Level.SEVERE, null, ex);
                            }


                            Connection conn11 = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "115314023");
                            Statement statement4 = null;
                            try {
                                statement4 = conn11.createStatement();
                                String sql4 = "insert into nota values ('"
                                        + pembelian.getNomornota() + "','" + pembelian.getTanggal()
                                        + "')";
                                statement4.executeUpdate(sql4);
                                conn11.close();
                            } catch (SQLException ex) {
                                Logger.getLogger(Pembelian.class.getName()).log(Level.SEVERE, null, ex);
                            }




                            Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "115314023");
                            java.sql.Statement statement = null;
                            try {
                                statement = conn.createStatement();
                            } catch (SQLException ex) {
                                Logger.getLogger(Pembelian.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            String sqQ = "select * from barang where id_barang = " + pembelian.getId()[i];
                            ResultSet result = null;
                            try {
                                result = statement.executeQuery(sqQ);
                            } catch (SQLException ex) {
                                Logger.getLogger(Pembelian.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            try {
                                while (result.next()) {
                                    Barang temp = new Barang();
                                    temp.setId_barang(result.getString("id_barang"));
                                    temp.setNama_barang(result.getString("nama_barang"));
                                    int y = Integer.parseInt(result.getString("stok"));
                                    int x = Integer.parseInt(result.getString("harga"));
                                    temp.setHarga_barang(x);
                                    temp.setStok(y);
                                    if (temp.getStok() >= 0) {
                                        out.println("<tr>");
                                        out.println("<td><center>" + temp.getId_barang() + "</center></td>");
                                        out.println("<td>" + temp.getNama_barang() + "</td>");
                                        out.println("<td><center>" + temp.getStok() + "</center></td>");
                                        out.println("<td><center>Rp " + temp.getHarga_barang() + ",00</center></td>");
                                        out.println("</tr>");


                                        Statement statement5 = null;
                                        try {
                                            statement5 = conn.createStatement();
                                            for (int k = 0; k < pembelian.getId().length; k++) {
                                                String sql5 = "insert into daftarpembelian values ('"
                                                        + pembelian.getNomornota() + "'," + pembelian.getId()[i]
                                                        + ")";
                                                statement5.executeUpdate(sql5);
                                                conn.close();
                                            }
                                        } catch (SQLException ex) {
                                            Logger.getLogger(Pembelian.class.getName()).log(Level.SEVERE, null, ex);
                                        }


                                        pembelian.setHarga_total(pembelian.getHarga_total() + temp.getHarga_barang());
                                        pembelian.setJumlah_barang(pembelian.getJumlah_barang() + 1);


                                    } else {
                                        temp.setHarga_barang(0);
                                        out.println("<tr>");
                                        out.println("<td><center>" + temp.getId_barang() + "</center></td>");
                                        out.println("<td>" + temp.getNama_barang() + "</td>");
                                        out.println("<td><center>" + "Stock Tidak Tersedia" + "</center></td>");
                                        out.println("<td><center>Rp " + temp.getHarga_barang() + ",00</center></td>");
                                        out.println("</tr>");

                                        Statement statement8 = null;
                                        try {
                                            statement8 = conn.createStatement();
                                            for (int j = 0; j < pembelian.getId().length; j++) {
                                                String sql8 = "update barang set stok = stok + 1 where id_barang = " + pembelian.getId()[i];
                                                statement8.executeUpdate(sql8);
                                                conn.close();
                                            }
                                        } catch (SQLException ex) {
                                            Logger.getLogger(Pembelian.class.getName()).log(Level.SEVERE, null, ex);
                                        }
                                    }







                                }
                            } catch (SQLException ex) {
                                Logger.getLogger(Pembelian.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            try {
                                conn.close();
                            } catch (SQLException ex) {
                            }
                        }
                    }


                    if (pembelian.getHarga_total() > 0) {
                        Connection conn7 = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "115314023");
                        Statement statement7 = null;
                        try {
                            statement7 = conn7.createStatement();
                            String sql7 = "insert into transaksi values ('"
                                    + pembelian.getNomornota() + "','" + pembelian.getKtp() + "','"
                                    + pembelian.getJenis_barang() + "'," + pembelian.getJumlah_barang() + ","
                                    + pembelian.getHarga_total() + ")";

                            statement7.executeUpdate(sql7);
                            conn7.close();

                        } catch (SQLException ex) {
                            Logger.getLogger(Pembelian.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        out.println("<center><h2>Nomor Nota Pembelian : " + pembelian.getNomornota() + "<br>");
                        out.println("Tanggal Transaksi : " + pembelian.getTanggal() + "<br></h2></center>");


                    } else {
                        Connection conn10 = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "115314023");
                        Statement statement10 = null;
                        try {
                            statement10 = conn10.createStatement();
                            String sql10 = "delete from nota where nomornota='"
                                    + pembelian.getNomornota() + "'";
                            statement10.executeUpdate(sql10);
                            conn10.close();
                        } catch (SQLException ex) {
                            Logger.getLogger(Pembelian.class.getName()).log(Level.SEVERE, null, ex);
                        }

                         Connection conn11 = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "hr", "115314023");
                        Statement statement11 = null;
                        try {
                            statement11 = conn11.createStatement();
                            String sql11 = "delete from datapembeli where ktp='"
                                    + pembelian.getKtp() + "'";
                            statement11.executeUpdate(sql11);
                            conn11.close();
                        } catch (SQLException ex) {
                            Logger.getLogger(Pembelian.class.getName()).log(Level.SEVERE, null, ex);
                        }

                        out.println("<CENTER><h1>STOK HABIS TRANSAKSI GAGAL</H1></CENTER>");
                    }








                    out.println("<tr>");
                    out.println("<td colspan = 3><center>TOTAL PEMBAYARAN</center></td>");
                    out.println("<td><center>Rp " + pembelian.getHarga_total() + ",00</center></td>");
                    out.println("</tr>");
                    out.print("</table>");
                    out.println("</center>");
                    out.println("</b>");


        %>
    </body>
</html>